// votePage/pages/list/list.js
const app = getApp();
import { wxLogin } from '../../../utils/utils'
// console.log(wxLogin)
Page({
  /**
   * 页面的初始数据
   */
  data: {
    worksList:[],
    loading:false,
    pageNum: 1,
    keyword:'',
    sort:'down',
    banner:'',
    id:'',
    is_vote_btn:0,
    vote_remainder_num:0
  },
  
  onLoad: function (options) {
    var _this = this;
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    // 作品列表
    wxLogin(_this.worksList());
  },

// 搜索
searchBtn: function () {
  var { keyword } = this.data;
  if(keyword) {
    this.setData({
      pageNum: 1,
      worksList: ''
    })
    this.worksList();
  }
},

voteTo:function(e) {
  var _this = this;
  var { id } = e.currentTarget.dataset;
  var { index } = e.currentTarget.dataset;
  wx.request({
    url: app.globalData.baseNew + 'api/activity/vote/' + id,
    header: {
      Authorization: "Bearer " + wx.getStorageSync('c-token'),
    },
    method:'post',
    data: {},
    success(res) {
      wx.hideLoading()
      if (res.statusCode == 200) {
          //console.log(res.data.data)
          //console.log(index);
          // _this.setData({
          //   vote_remainder_num: res.data.data.data.vote_remainder_num
          // })
          let worksList = _this.data.worksList;
          worksList[index].today_already_vote  =  worksList[index].today_already_vote + 1
          _this.setData({
            worksList: worksList
          })
          _this.setData({
            vote_remainder_num: res.data.data.residue_num
          })
          wx.showToast({
            title: '投票成功',
            icon: 'none',
            duration: 2000
          })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
      }
    }
  })
},

onShareAppMessage: function () {
  var { id } = this.data;
  console.log(id)
  return {
    title: '书香长江投票活动',
    path: '/votePage/pages/list/list?id=' + id
  }
},

// onShareTimeline: function () {
//   var { id } = this.data;
//   return {
//     title: '书香长江投票活动',
//     path: '/votePage/pages/list/list?id=' + id
//   }
// },
// preventDefault: function(event) {
//   // 阻止默认事件
//   console.log(event)
//   event.preventDefault();
// },

goDtail:function(e) {
  var { id } = e.currentTarget.dataset;
  // console.log(id);
  wx.navigateTo({
     url: '/votePage/pages/detail/detail?id=' + id,
  })
},

bindKeyInput: function (e) {
  this.setData({
    keyword: e.detail.value
  })
},

sortBtn:function() {
  var { sort } = this.data;
  if( sort == 'up') {
    this.setData({
      sort: "down",
    })
  } else {
    this.setData({
      sort: "up",
    }) 
  }

  this.setData({
    pageNum: 1,
    worksList: ''
  })
  this.worksList();
},

worksList:function(){
    // console.log(123)
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      loading:false
    })
    var _this = this;
    var { pageNum, keyword, worksList, id, sort } = this.data;
    // 获取本地数据
         wx.request({
          url: app.globalData.baseNew + 'api/activity/' + id,
          header: {
            Authorization: "Bearer " + wx.getStorageSync('c-token'),
          },
          data: {
            keyword: "",
            page:pageNum,
            // page_count:50,
            sort:sort,
            keyword:keyword
          },
          success(res) {
            wx.hideLoading()
            console.log(res.data.data.data.data)
            if (res.statusCode == 200) {
              if(res.data.data.data.data.length > 0) {
                _this.setData({
                  vote_remainder_num: res.data.data.data.vote_remainder_num
                })
                const vote_btn = res.data.data.data.data[0].is_vote_btn
                const banner = res.data.data.data.data[0].banner_image

                _this.setData({
                  is_vote_btn: vote_btn
                })
                _this.setData({
                  banner: banner
                })

              }
              _this.setData({
                worksList: [...worksList, ...res.data.data.data.data]
              })
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 2000
              })
            }
            _this.setData({
              loading:_this.data.worksList.length===0
            })
          }
        })
  },

  onReachBottom: function () {
    var { pageNum } = this.data;
    pageNum ++;
    this.setData({
      pageNum
    })
    this.worksList();
  }
})